/**
 * 定义线程池的拒绝策略枚举类
 * 当线程池无法再处理新任务时，会根据设定的拒绝策略来处理新提交的任务
 */
package com.lancer.dynamic.thread.pool.lancerenum;

import lombok.Getter;

import java.util.LinkedHashMap;

/**
 * 丢弃策略枚举类
 */
@Getter
public enum RejectEnum {

    /**
     * 由提交任务的线程自己来执行任务
     */
    CALLER_RUNS("CALLER_RUNS","由提交任务的线程自己来执行任务"),

    /**
     * 直接丢弃新提交的任务，不予处理
     */
    DISCARD("DISCARD","直接丢弃新提交的任务，不予处理"),

    /**
     * 丢弃 oldest（最旧）的任务，并尝试重新提交当前任务
     */
    DISCARD_OLDEST("DISCARD_OLDEST","丢弃 oldest（最旧）的任务，并尝试重新提交当前任务"),

    /**
     * 抛出异常，终止线程池
     */
    ABORT("ABORT","抛出异常，终止线程池");

    private final String rejectName;
    private final String rejectText;

    RejectEnum(String rejectName, String rejectText) {
        this.rejectName = rejectName;
        this.rejectText = rejectText;
    }
    // 获取拒绝策略List
    public static LinkedHashMap<String,String> getRejectEnumList() {
        LinkedHashMap<String,String> rejectEnumList = new LinkedHashMap<>();
        for (RejectEnum rejectEnum : RejectEnum.values()) {
            rejectEnumList.put(rejectEnum.getRejectName(),rejectEnum.getRejectText());
        }
        return rejectEnumList;
    }

    public static RejectEnum queryName(String simpleName) {
//        RejectedExecutionHandler handler = switch (policyName.toUpperCase()) {
//            case "CALLER_RUNS" -> new ThreadPoolExecutor.CallerRunsPolicy();
//            case "DISCARD" -> new ThreadPoolExecutor.DiscardPolicy();
//            case "DISCARD_OLDEST" -> new ThreadPoolExecutor.DiscardOldestPolicy();
//            default -> new ThreadPoolExecutor.AbortPolicy();
//        };
//
        return switch (simpleName) {
            case "CallerRunsPolicy" -> CALLER_RUNS;
            case "DiscardPolicy" -> DISCARD;
            case "DiscardOldestPolicy" -> DISCARD_OLDEST;
            case "AbortPolicy" -> ABORT;
            default -> ABORT;
        };
    }
}
